package com.li.time;

import org.junit.Test;


import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

/**
 * @author 喂S别闹
 * @create 2022/4/21-14:37
 * @Version 1.0
 * @Description: 时间的新特性
 */
public class LocalDateTimeTest {
    /**
     * @Date: 2022/4/21 14:38
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description:LocalDate LocalTime LocateDateTime
     */
    @Test
    public void test1() {
//        获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        LocalDateTime of = LocalDateTime.of(2022, 12, 22, 13, 13, 22);
        System.out.println(of);

//        加减时间
        LocalDateTime localDateTime = of.plusYears(2);
        System.out.println(localDateTime);

        LocalDateTime localDateTime1 = of.minusMonths(2);
        System.out.println(localDateTime1);
    }

    /**
     * @Date: 2022/4/21 14:42
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description: Instant 时间戳（以Unix 元年，1970开始
     */
    @Test
    public void test2() {
        Instant now = Instant.now();  //默认获取UTC时区的时间
        System.out.println(now);

//        设置偏移量
        ZonedDateTime time = now.atZone(ZoneOffset.ofHours(8));
        System.out.println(time);

//        转为时间戳
        System.out.println(now.toEpochMilli());
    }

    /**
     * @Date: 2022/4/21 14:48
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description: 计算两个时间之间的间隔    Duration
     * 计算两个日期之间的间隔   Period
     */
    @Test
    public void test3() {
        Instant ins1 = Instant.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant ins2 = Instant.now();

        Duration between = Duration.between(ins1, ins2);
        System.out.println(between.toMillis());

        System.out.println("----------------");

        LocalDateTime lt1 = LocalDateTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalDateTime ld2 = LocalDateTime.now();
        System.out.println(Duration.between(lt1, ld2).toMillis());

//        日期：
        LocalDate of = LocalDate.of(2012, 12, 12);
        LocalDate now = LocalDate.now();
        Period between1 = Period.between(of, now);
        System.out.println(between1);
        System.out.println(between1.getYears());
        System.out.println(between1.getMonths());
        System.out.println(between1.getDays());


    }

    /**
     * @Date: 2022/4/21 14:56
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description:时间矫正器
     */
    @Test
    public void test4() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
//        将当前月中的天指定为10
        LocalDateTime localDateTime = ldt.withDayOfMonth(10);
        System.out.println(localDateTime);
//        下一个周yi
        LocalDateTime with = ldt.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println(with);
    }

    /**
     * @Date: 2022/4/21 15:07
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description:时间格式化 DateTimeFormatter
     */
    @Test
    public void test5() {
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();

        String format = ldt.format(dtf);
        System.out.println(format);

//        自己定义
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(dtf2.format(ldt));
    }

    /**
     * @Date: 2022/4/21 15:14
     * @Auther: 喂S别闹
     * @param:
     * @Return: void
     * @Description: 指定时区   ZonedDate  ZonedTime ZonedDateTime
     */
    @Test
    public void test6() {
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
        System.out.println(now);

        LocalDateTime now1 = LocalDateTime.now();
        ZonedDateTime zonedDateTime = now1.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);
    }
}
